package com.fengchao.api.controller.v1;

import com.alibaba.fastjson.JSON;
import com.fengchao.api.config.Constant;
import com.fengchao.api.service.CfgService;
import com.fengchao.api.service.SvyService;
import com.fengchao.api.service.UsrService;
import com.fengchao.model.cfg.CfgRegion;
import com.fengchao.model.entity.comm.CustomBizException;
import com.fengchao.model.entity.comm.Message;
import com.fengchao.model.entity.comm.ResponseParameter;
import com.fengchao.util.common.CommonValidator;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static com.fengchao.api.config.Constant.GET_SUCCESS;
import static com.fengchao.api.config.Constant.PARAMETER_IS_NOT_COMPLETE;

/**
 * Created by wangnian on 2016/7/13.
 */
@RestController
@RequestMapping("/v1/svy")
public class SvyController {

    @Autowired
    private CfgService cfgService;
    @Autowired
    private SvyService svyService;
    @Autowired
    private UsrService usrService;

    private static Logger logger = LoggerFactory.getLogger(SvyController.class);

    @Deprecated
    @RequestMapping(value = "/addWishArtist", method = RequestMethod.POST)
    @ApiOperation(value = "(APP/WEB) <1.4版本 添加心动艺人", notes = "返回参数:</br>成功:</br>{status: SUCCESS, message: 添加成功}" +
            "</br>重新登录:</br>{status:RELOGIN, message:Token失效}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}</br>{status: FAILURE, message: 该艺人及城市每日限投一次}")
    public ResponseParameter addWishArtist(@ApiParam(value = "(APP/WEB必填)类型, APP/WEB", required = true) @RequestHeader String type,
                                           @ApiParam(value = "(APP/WEB必填)用户token", required = true) @RequestHeader String token,
                                           @ApiParam(value = "(APP/WEB必填)艺人id", required = true) @RequestParam Integer wishArtistId,
                                           @ApiParam(value = "(APP/WEB必填)城市名称", required = true) @RequestParam String wishCityName) {
        logger.info("添加心动艺人请求参数=type:{},token：{},wishArtistId:{},wishCityName:{}", type, token, wishArtistId, wishCityName);
        CfgRegion cfgRegion = cfgService.getCfgRegionByName(wishCityName);
        if (!CommonValidator.isStringsNotNull(type) || cfgRegion == null) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        Integer userId = usrService.getUserId(type, token, true);
        Integer wishCityId = cfgRegion.getId();
        ResponseParameter responseParameter = svyService.addWishArtist(userId, wishCityId, wishArtistId);
        logger.info("添加心动艺人响应参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }

    @Deprecated
    @RequestMapping(value = "/getWishArtist/{page}/{count}", method = RequestMethod.GET)
    @ApiOperation(value = "(APP/WEB) <1.4版本 显示心动艺人的排行", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功, </br>data:{</br>&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;image: http://i.gtimg.cn/music/photo/mid_singer_300/a/e/000t2qd13dLpae.jpg (头像),</br>&nbsp;&nbsp;&nbsp;&nbsp;cityName: 上海(城市名字)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;name: 权志龙(クォン・ジヨン、Kwon Ji Yong)(艺人姓名),</br>&nbsp;&nbsp;&nbsp;&nbsp;count: 12(投票次数),</br>&nbsp;&nbsp;&nbsp;&nbsp;ranking: 01(排序),</br>&nbsp;&nbsp;&nbsp;&nbsp;region: 韩国首尔特别市龙山区(出生地区)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    public ResponseParameter getWishArtist(@ApiParam(value = "(APP/WEB必填)类型, APP/WEB", required = true) @RequestHeader String type,
                                           @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                           @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("显示心动艺人的排行请求参数=type:{},page:{},count:{}", type, page, count);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        List<Map<String, Object>> resultList = svyService.getWishArtistResult(page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", resultList);
        responseParameter.setData(map);
        logger.debug("显示心动艺人的排行响应参数= {}", JSON.toJSON(responseParameter));
        logger.info("显示心动艺人的排行响应参数= 返回条数:{}", resultList.size());
        return responseParameter;
    }


    @RequestMapping(value = "/vm1/addWishArtist", method = RequestMethod.POST)
    @ApiOperation(value = "(APP/WEB) >=1.4版本 添加心动艺人", notes = "返回参数:</br>成功:</br>{status: SUCCESS, message: 添加成功}" +
            "</br>重新登录:</br>{status:RELOGIN, message:Token失效}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}</br>{status: FAILURE, message: 该艺人及城市每日限投一次}")
    public ResponseParameter addWishArtistVm11(@ApiParam(value = "(APP/WEB必填)类型, APP/WEB", required = true) @RequestHeader String type,
                                               @ApiParam(value = "(APP/WEB必填)用户token", required = true) @RequestHeader String token,
                                               @ApiParam(value = "(APP/WEB必填)艺人id", required = true) @RequestParam Integer wishArtistId,
                                               @ApiParam(value = "(APP/WEB必填)城市id", required = true) @RequestParam Integer wishCityId) {
        logger.info("vm1.1添加心动艺人请求参数=type:{},token：{},wishArtistId:{},wishCityId:{}", type, token, wishArtistId, wishCityId);
        Integer userId = usrService.getUserId(type, token, true);
        ResponseParameter responseParameter = svyService.addWishArtist(userId, wishCityId, wishArtistId);
        logger.info("vm1.1添加心动艺人响应参数= {}", JSON.toJSON(responseParameter));
        return responseParameter;
    }


    @RequestMapping(value = "/vm1/getWishArtist/{page}/{count}", method = RequestMethod.GET)
    @ApiOperation(value = "(APP/WEB) >=1.4版本 显示心动艺人的排行", notes = "返回参数:</br>成功:</br>{status: SUCCESS, </br>message: 获取成功, </br>data:{</br>&nbsp;info:[</br>&nbsp;&nbsp;&nbsp;{</br>&nbsp;&nbsp;&nbsp;&nbsp;image: http://i.gtimg.cn/music/photo/mid_singer_300/a/e/000t2qd13dLpae.jpg (头像),</br>&nbsp;&nbsp;&nbsp;&nbsp;cityName: 上海(城市名字)," +
            "</br>&nbsp;&nbsp;&nbsp;&nbsp;name: 权志龙(クォン・ジヨン、Kwon Ji Yong)(艺人姓名),</br>&nbsp;&nbsp;&nbsp;&nbsp;count: 12(投票次数),</br>&nbsp;&nbsp;&nbsp;&nbsp;ranking: 01(排序),</br>&nbsp;&nbsp;&nbsp;&nbsp;region: 韩国首尔特别市龙山区(出生地区)</br>&nbsp;&nbsp;&nbsp;}</br>&nbsp;&nbsp;]</br>&nbsp;}</br>}" +
            "</br>失败:</br>{status: FAILURE, message: 参数不全}")
    public ResponseParameter getWishArtistVm11(@ApiParam(value = "(APP/WEB必填)类型, APP/WEB", required = true) @RequestHeader String type,
                                               @ApiParam(value = "(APP/WEB必填)第几页", required = true) @PathVariable("page") Integer page,
                                               @ApiParam(value = "(APP/WEB必填)显示多少行", required = true) @PathVariable("count") Integer count) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("vm1.1显示心动艺人的排行请求参数=type:{},page:{},count:{}", type, page, count);
        if (!CommonValidator.isStringsNotNull(type)) {
            throw new CustomBizException(PARAMETER_IS_NOT_COMPLETE);
        }
        List<Map<String, Object>> resultList = svyService.getWishArtistResultV1(page, count);
        Map<Object, Object> map = new HashMap<>();
        map.put("info", resultList);
        responseParameter.setData(map);
        logger.debug("vm1.1显示心动艺人的排行响应参数= {}", JSON.toJSON(responseParameter));
        logger.info("vm1.1显示心动艺人的排行响应参数= 返回条数:{}", resultList.size());
        return responseParameter;
    }

    /**
     * 获取城市/热门城市
     *
     * @param type
     * @return
     */
    @ApiOperation(value = "(APP) >=1.4版本 获取城市/热门城市接口", notes = "返回参数: </br>成功: id name")
    @RequestMapping(value = "/getSvyCity", method = RequestMethod.GET)
    public ResponseParameter getSvyCity(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取城市/热门城市接口请求参数: type:{}", type);
        Map resultMap = new HashMap();
        resultMap.put("info", svyService.getSvyCity());
        responseParameter.setData(resultMap);
        logger.debug("获取城市/热门城市响应参数:{}", JSON.toJSON(responseParameter));
        logger.info("获取城市/热门城市响应参数:{}", responseParameter.getStatus());
        return responseParameter;
    }

    /**
     * 获取城市版本更新时间接口
     *
     * @param type
     * @return
     */
    @ApiOperation(value = "(APP)获取城市版本更新时间接口", notes = "返回参数 date(城市版本更新时间)")
    @RequestMapping(value = "/getSvyCityUpdateDate", method = RequestMethod.GET)
    public ResponseParameter getSvyCityUpdateDate(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取城市版本更新时间接口请求参数: type:{}", type);
        Map resultMap = new HashMap();
        Map date = new HashMap<>();
        date.put("date", cfgService.findCfgApiConfigByKey(Constant.cityListUpdateDate).getValue());
        resultMap.put("info", date);
        responseParameter.setData(resultMap);
        logger.error("获取城市版本更新时间接口响应参数:{}", JSON.toJSON(responseParameter));
        logger.info("获取城市版本更新时间接口响应参数:{}", responseParameter.getStatus());
        return responseParameter;
    }

    /**
     * 根据心动艺人id获取各个城市的排名接口
     *
     * @param type
     * @param token
     * @param withArtistId
     * @return
     */
    @ApiOperation(value = "(APP)根据心动艺人id获取各个城市的排名接口(最多10条数据)", notes = "返回参数: 成功: 城市名(cityName)、显示票数(voteCount)、显示目标(voteTarget)、是否当天已投票(isVote)、城市排名(ranking)")
    @RequestMapping(value = "/getSvyCityByArtistId/{withArtistId}", method = RequestMethod.GET)
    public ResponseParameter getSvyCityByArtistId(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type,
                                                  @ApiParam(value = "(必填)用户token", required = true) @RequestHeader String token,
                                                  @ApiParam(value = "(必填)心动艺人id, APP", required = true) @PathVariable Integer withArtistId) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("根据艺人id获取各个城市的排名接口请求参数: type:{}, token:{}", type, token);
        Integer userId = usrService.getUserId(type, token, true);
        Map resultMap = new HashMap();
        resultMap.put("info", svyService.getSvyCityByArtistId(withArtistId, userId));
        responseParameter.setData(resultMap);
        logger.debug("根据艺人id获取各个城市的排名接口响应参数:{}", JSON.toJSON(responseParameter));
        logger.info("根据艺人id获取各个城市的排名接口响应参数:{}", responseParameter.getStatus());
        return responseParameter;
    }

    /**
     * 获取5个城市的排名前十的艺人的接口
     *
     * @param type
     * @param token
     * @return
     */
    @ApiOperation(value = "(APP)获取5个城市的排名前十的艺人的接口(每个城市最多10条数据)", notes = "返回参数: 成功:城市ID(cityId) 城市名(cityName)、城市的排名(ranking)、城市的所有票(voteCount)、城市上升状态 RISE：上升；FALL：下降；HOLD：无变化 (rankingChangeStatus) " +
            "</br> 艺人id (artistId)  艺人头像(headIcon)、名字(artistName)、票数(voteCount)、排名数(artistRank)、是否可投票(isVote)")
    @RequestMapping(value = "/getArtistByCity", method = RequestMethod.GET)
    public ResponseParameter getArtistByCity(@ApiParam(value = "(必填)类型, APP", required = true) @RequestHeader String type,
                                             @ApiParam(value = "(必填)用户token", required = true) @RequestHeader String token) {
        ResponseParameter responseParameter = new ResponseParameter(Message.SUCCESS, GET_SUCCESS);
        logger.info("获取5个城市的排名前十的艺人的接口请求参数: type:{}, token:{}", type, token);
        Integer userId = usrService.getUserId(type, token, true);
        Map resultMap = svyService.getArtistByCity(userId);
        responseParameter.setData(resultMap);
        logger.debug("获取5个城市的排名前十的艺人的接口响应参数:{}", JSON.toJSON(responseParameter));
        logger.info("获取5个城市的排名前十的艺人的接口响应参数:{}", responseParameter.getStatus());
        return responseParameter;
    }
}
